package com.ssm.controller;

import com.ssm.entity.Teacher;
import com.ssm.service.TeacherService;
import com.ssm.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 教师管理控制器
 */
@RestController
@RequestMapping("/api/teachers")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 获取所有教师
     * @return 教师列表
     */
    @GetMapping
    @Cacheable(value = "teacherList", key = "'all'")
    public Result<List<Teacher>> getAllTeachers() {
        List<Teacher> teachers = teacherService.findAll();
        return Result.success(teachers);
    }

    /**
     * 根据ID获取教师
     * @param id 教师ID
     * @return 教师信息
     */
    @GetMapping("/{id}")
    @Cacheable(value = "teacher", key = "#id")
    public Result<Teacher> getTeacherById(@PathVariable Integer id) {
        Teacher teacher = teacherService.findById(id);
        if (teacher != null) {
            return Result.success(teacher);
        } else {
            return Result.fail("教师不存在");
        }
    }

    /**
     * 添加教师
     * @param teacher 教师信息
     * @return 添加结果
     */
    @PostMapping
    @CacheEvict(value = {"teacherList", "teacher"}, allEntries = true)
    public Result<Teacher> addTeacher(@Valid @RequestBody Teacher teacher) {
        boolean success = teacherService.add(teacher);
        if (success) {
            return Result.success(teacher);
        } else {
            return Result.fail("添加教师失败");
        }
    }

    /**
     * 更新教师
     * @param id 教师ID
     * @param teacher 教师信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @CacheEvict(value = {"teacherList", "teacher"}, allEntries = true)
    public Result<Teacher> updateTeacher(@PathVariable Integer id, @Valid @RequestBody Teacher teacher) {
        teacher.setTeacherId(id);
        boolean success = teacherService.update(teacher);
        if (success) {
            return Result.success(teacher);
        } else {
            return Result.fail("更新教师失败");
        }
    }

    /**
     * 删除教师
     * @param id 教师ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @CacheEvict(value = {"teacherList", "teacher"}, allEntries = true)
    public Result<Void> deleteTeacher(@PathVariable Integer id) {
        boolean success = teacherService.delete(id);
        if (success) {
            return Result.success();
        } else {
            return Result.fail("删除教师失败");
        }
    }

    /**
     * 根据部门查询教师
     * @param department 部门名称
     * @return 教师列表
     */
    @GetMapping("/department/{department}")
    @Cacheable(value = "teacherList", key = "'department:' + #department")
    public Result<List<Teacher>> getTeachersByDepartment(@PathVariable String department) {
        List<Teacher> teachers = teacherService.findByDepartment(department);
        return Result.success(teachers);
    }
} 